﻿using Sunny.UI;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Commons.SerializeHelper;
using Ycjw.PipeLine.Map.Tools;
using Ycjw.PipeLine.Scenes;

namespace Ycjw.PipeLine
{
    class RibbonMenuRegister
    {
        DockPanel m_DockPanel;
        public void SetDockPanel(DockPanel dockPanel)
        {
            m_DockPanel = dockPanel;
        }
        static RibbonMenuRegister m_Instance;
        public static RibbonMenuRegister Instance
        {
            get
            {
                if (m_Instance == null)
                {
                    m_Instance = new RibbonMenuRegister();
                }
                return m_Instance;
            }
        }
        private void SetRibbonIcon(Ribbon ribbon)
        {
            foreach(RibbonTab tab in ribbon.Tabs)
            {
                foreach(var panel in tab.Panels)
                {
                    foreach(var item in panel.Items)
                    {
                        SetRibbonItemMouseHoverEvent(item);
                        string path = Commons.Paths.ApplicationResourcesDir + "\\ribbons\\" + item.Text + ".png";
                        if (!File.Exists(path))
                            path = Commons.Paths.ApplicationResourcesDir + "\\" + item.Text + ".png";
                        if (!File.Exists(path)) continue;
                        var img = System.Drawing.Image.FromFile(path);
                        if (item is RibbonButton)
                        {
                            var rb = item as RibbonButton;
                            rb.SmallImage = img;
                            rb.LargeImage = img;
                        }
                    }
                }
            }
        }
        private void SetRibbonItemMouseHoverEvent(RibbonItem item)
        {
            item.MouseEnter += new MouseEventHandler((i, e) => {
                var theItem = i as RibbonItem;
                FormMainRibbon.Instance.ShowInfo(theItem.Text);
            });
        }
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern int SetCursorPos(int x, int y);
        //模拟鼠标左键抬起 
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        private void SelectFirstTab()
        {
            var x = 40;
            var y = Control.MousePosition.Y;
            Point p = new Point(x, y);
            SetCursorPos(p.X, p.Y);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }
        public void AddTabs(Ribbon ribbon) {
            RibbonTab rt = new RibbonTab
            {
                Text = "实时监控"
            };
            ribbon.Tabs.Add(rt);
            rt.ActiveChanged += new EventHandler((sender, e) => {
                var realTimeRt = sender as RibbonTab;
                if (realTimeRt.Active)
                {
                    SelectFirstTab();
                    SceneAdapter.Instance.RealTime();
                }
            });
            rt = new RibbonTab
            {
                Text = "大屏展示"
            };
            ribbon.Tabs.Add(rt);
            rt.ActiveChanged += new EventHandler((sender, e) => {
                var realTimeRt = sender as RibbonTab;
                if (realTimeRt.Active)
                {
                    SelectFirstTab();
                    SceneAdapter.Instance.FullScreen();
                }
            });
        }


        public void RegisterByConfig(Ribbon ribbon)
        {
            SetRibbonIcon(ribbon);
            var dir = Commons.Paths.ApplicationConfigDir + "\\menus";
            var tabs = Directory.GetDirectories(dir);
            foreach (var tab in tabs)
            {
                RibbonTab rt = new RibbonTab() { Name = tab.SplitLast("-"), Text = tab.SplitLast("-") };
                var panels = Directory.GetDirectories(tab);
                foreach (var panel in panels)
                {
                    RibbonPanel rp = new RibbonPanel() { Name = panel.SplitLast("-"), Text = panel.SplitLast("-") };
                    var items = Directory.GetFiles(panel);
                    foreach (var item in items)
                    {
                        if (item.Contains("RibbonButton"))
                        {
                            TextReader tr = File.OpenText(item);
                            RibbonButton rb = JsonHepler.Json_Deserialize<RibbonButton>(tr.ReadToEnd());
                            if(rb.Text == "按管径统计")
                            {

                            }
                            var imagePath = Commons.Paths.ApplicationResourcesDir + "\\" + System.IO.Path.GetFileNameWithoutExtension(item) + ".png";
                            if (!File.Exists(imagePath))
                                imagePath = Commons.Paths.ApplicationResourcesDir + "\\" + rb.Text + ".png";
                            if (File.Exists(imagePath))
                            {
                                rb.Image = System.Drawing.Image.FromFile(imagePath);
                            }
                            else
                            {
                                imagePath = Commons.Paths.ApplicationResourcesDir + "\\RibbonButton-defalut.png";
                                rb.Image = System.Drawing.Image.FromFile(imagePath);
                            }
                            rb.MinSizeMode = RibbonElementSizeMode.Large;
                            rb.MaxSizeMode = RibbonElementSizeMode.Large;
                            rp.Items.Add(rb);
                            rb.Click += Rb_Click;
                            SetRibbonItemMouseHoverEvent(rb);
                        }
                    }
                    rt.Panels.Add(rp);
                }
                ribbon.Tabs.Add(rt);
            }
        }

        private void Rb_Click(object sender, EventArgs e)
        {
            var ribbonButton = sender as RibbonButton;
            if (ribbonButton.Tag == null) return;

            Conditioner.Instance.SetDockPanel(m_DockPanel);
            Conditioner.Instance.SetSceneEx(SceneAdapter.Instance.SceneEx);
            Conditioner.Instance.SetSceneControl(SceneAdapter.Instance.SceneControl);
            Conditioner.Instance.MessageInterface = FormMainRibbon.Instance;
            Conditioner.Instance.CreateCondition(ribbonButton.Tag.ToString());
        }

        RibbonMenuRegister()
        {

        }
    }
}
